package rdc.simonalisa.util;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.TabExpander;


public class TableScriptGenerator {

	List<String> listColumnName;
	
	String tableName;
	
	public TableScriptGenerator(){
		listColumnName = new ArrayList<String>();
		
	}
	
	public void parseColum(String param){
		String[] arrString = param.split("\t");
		for(int i=0; i<arrString.length; i++){
			//System.out.println(arrString[i]);
			if(arrString[i].indexOf("<")>0){
				listColumnName.add(arrString[i].substring(0,arrString[i].indexOf("<")));
			}else{
				listColumnName.add(arrString[i]);
			}
		}
	}
	
	public void createTableScript(){
		
		
		String columnScript = createScriptColumn();
		
		String output = 
			"CREATE TABLE " +  tableName.toUpperCase() + "\n" +
			"(\n" + 
			"\tUNIQID \t NUMBER \t NOT NULL, \n" +
			columnScript +
			")\n";
		
		String tablespace = 
			"TABLESPACE USERS \n" +
			"PCTUSED    0	 \n" +
			"PCTFREE    10	 \n" +
			"INITRANS   1	 \n" +
			"MAXTRANS   255	 \n" +
			"STORAGE    (	 \n" +
			"            INITIAL          64K	 \n" +
			"            MINEXTENTS       1	 \n" +
			"            MAXEXTENTS       2147483645	 \n" +
			"            PCTINCREASE      0	 \n" +
			"            BUFFER_POOL      DEFAULT	 \n" +
			"           )	 \n" +
			"LOGGING 	 \n" +
			"NOCOMPRESS 	 \n" +
			"NOCACHE	 \n" +
			"NOPARALLEL	 \n" +
			"MONITORING; \n\n ";
		
		String uniqIndex = 
			"CREATE UNIQUE INDEX " + tableName.toUpperCase() + "_PK ON " + tableName.toUpperCase()  + " \n" +
			"(UNIQID) \n" +
			"LOGGING \n" +
			"TABLESPACE USERS \n" +
			"PCTFREE    10 \n" +
			"INITRANS   2 \n" +
			"MAXTRANS   255 \n" +
			"STORAGE    ( \n" +
			"            INITIAL          64K \n" +
			"            MINEXTENTS       1 \n" +
			"            MAXEXTENTS       2147483645 \n" +
			"            PCTINCREASE      0 \n" +
			"            BUFFER_POOL      DEFAULT \n" +
			"           ) \n" +
			"NOPARALLEL; \n\n";
		
		String alterTable = 
			"ALTER TABLE " + tableName.toUpperCase() + " ADD ( \n " +
			"		  CONSTRAINT " + tableName.toUpperCase() + "_PK \n " +
			"		 PRIMARY KEY \n " +
			"		 (UNIQID) \n " +
			"		    USING INDEX \n " +
			"		    TABLESPACE USERS \n " +
			"		    PCTFREE    10 \n " +
			"		    INITRANS   2 \n " +
			"		    MAXTRANS   255 \n " +
			"		    STORAGE    ( \n " +
			"		                INITIAL          64K \n " +
			"		                MINEXTENTS       1 \n " +
			"		                MAXEXTENTS       2147483645 \n " +
			"		                PCTINCREASE      0 \n " +
			"		               ));    \n ";
		
		System.out.println(output + tablespace + uniqIndex + alterTable);
	}
	
	private String createScriptColumn(){
		String retVal = "";
		
		String columnType = "";
		String columnName= "";
		
		for(int i=0; i<listColumnName.size(); i++){
			columnName = listColumnName.get(i);
			if(columnName.trim().equals("CLLI"))
				columnType = "VARCHAR2(100 BYTE)";
			else if(columnName.trim().equals("SWREL"))
				columnType = "VARCHAR2(100 BYTE)";
			else if(columnName.trim().equals("RPTDATE"))
				columnType = "DATE";
			else if(columnName.trim().equals("RPTIME"))
				columnType = "DATE";
			else if(columnName.trim().equals("TZ"))
				columnType = "VARCHAR2(25 BYTE)";
			else if(columnName.trim().equals("RPTTYPE"))
				columnType = "VARCHAR2(100 BYTE)";
			else if(columnName.trim().equals("RPTPD"))
				columnType = "VARCHAR2(25 BYTE)";
			else if(columnName.trim().equals("IVALDATE"))
				columnType = "DATE";
			else if(columnName.trim().equals("IVALSTART"))
				columnType = "DATE";
			else if(columnName.trim().equals("NUMENTIDS"))
				columnType = "NUMBER";
			else if(columnName.trim().equals("STATUS"))
				columnType = "VARCHAR2(25 BYTE)";
			else
				columnType = "NUMBER";
			
			
			retVal = retVal + 
				"\t" + columnName + "\t\t" +  columnType + ",\n";
			
		}
		retVal = retVal.substring(0, retVal.lastIndexOf(",")) + "\n";
		
		return retVal;
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
}
